package com.example.demo.controller.surperController;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.Mapper.CooperativeFarmMapper;
import com.example.demo.Mapper.SurperMapper.CooperativeFarmSurperMapper;
import com.example.demo.base.Result;
import com.example.demo.entity.CooperativeFarm;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/cooperativeFarmSurper")
public class CooperativeFarmSurperController {
    @Resource
    com.example.demo.Mapper.SurperMapper.CooperativeFarmSurperMapper cooperativeFarmSurperMapper;
    /**
     * 获取数据库全部数据
     *
     * @return
     */
    @GetMapping("/all")
    public Result<?> findAll() {
        return Result.success(cooperativeFarmSurperMapper.selectList(null));
    }

    /**
     * 分页获取数据库数据
     *
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping(value = "/pageAll")
    public Result<?> findPageC(@RequestParam(defaultValue = "1") Integer pageNum,
                               @RequestParam(defaultValue = "10") Integer pageSize) {
        LambdaQueryWrapper<CooperativeFarm> wrapper = Wrappers.<CooperativeFarm>lambdaQuery();
        Page<CooperativeFarm> cooperativeFarmPage = cooperativeFarmSurperMapper.selectPage(new Page<>(pageNum, pageSize), wrapper);
        return Result.success(cooperativeFarmPage);
    }

    /**
     * 根据农场id添加信息
     * @param farmId
     * @param farmName
     * @param name
     * @param farmAdd
     * @param farmTel
     * @param farmPro
     * @param farmProfile
     * @param note
     * @return
     */
    @GetMapping(value = "/add")
    public Result<?> saveC(@RequestParam String farmId,
                           @RequestParam String farmName,
                           @RequestParam String name,
                           @RequestParam String farmAdd,
                           @RequestParam String farmTel,
                           @RequestParam String farmPro,
                           @RequestParam String farmProfile,
                           @RequestParam String note,
                           @RequestParam String tenantId) {
        if (farmId.isEmpty() || farmName.isEmpty() || name.isEmpty() || farmAdd.isEmpty() || farmTel.isEmpty()||farmPro.isEmpty()||farmProfile.isEmpty()) {
            return Result.error(0, "文本不能为空");
        }
        CooperativeFarm cooperativeFarm = new CooperativeFarm(Integer.valueOf(farmId),farmName,name,farmAdd,farmTel,farmPro,farmProfile,note,Integer.valueOf(tenantId));
        cooperativeFarmSurperMapper.insert(cooperativeFarm);
        return Result.success();
    }

    /**
     * 更新数据
     * @param farmId
     * @param farmName
     * @param name
     * @param farmAdd
     * @param farmTel
     * @param farmPro
     * @param farmProfile
     * @param note
     * @return
     */
    @GetMapping(value = "/update")
    public Result<?> updateById(@RequestParam String farmId,
                           @RequestParam String farmName,
                           @RequestParam String name,
                           @RequestParam String farmAdd,
                           @RequestParam String farmTel,
                           @RequestParam String farmPro,
                           @RequestParam String farmProfile,
                           @RequestParam String note,
                                @RequestParam String tenantId) {
        if (farmId.isEmpty() || farmName.isEmpty() || name.isEmpty() || farmAdd.isEmpty() || farmTel.isEmpty()||farmPro.isEmpty()||farmProfile.isEmpty()) {
            return Result.error(0, "文本不能为空");
        }
        CooperativeFarm cooperativeFarm = new CooperativeFarm(Integer.valueOf(farmId),farmName,name,farmAdd,farmTel,farmPro,farmProfile,note,Integer.valueOf(tenantId));
        cooperativeFarmSurperMapper.updateById(cooperativeFarm);
        return Result.success();
    }

    /**
     * 商品溯源码查找
     * @param id
     * @return
     */

    @GetMapping("/findById/{id}")
    public Result<?> findById(@PathVariable String id) {
        CooperativeFarm cooperativeFarm = cooperativeFarmSurperMapper.selectById(id);
        List<CooperativeFarm> cooperativeFarmList = new ArrayList<>();
        if(cooperativeFarm != null)
        {
            cooperativeFarmList.add(cooperativeFarm);
            return Result.success(cooperativeFarmList);
        }
        return Result.error(0, "没有查询到相关数据");
    }

    /**
     * 根据农场名称模糊查找
     * @param farmName
     * @return
     */
    @GetMapping("/findByFarmName/{farmName}")
    public Result<?> findByFarmName(@PathVariable String farmName) {
        LambdaQueryWrapper<CooperativeFarm> wrapper = Wrappers.<CooperativeFarm>lambdaQuery();
        wrapper.like(CooperativeFarm::getFarmName,farmName);
        List<CooperativeFarm> cooperativeFarmList = cooperativeFarmSurperMapper.selectList(wrapper);
        if(cooperativeFarmList.size() > 0)
            return Result.success(cooperativeFarmList);
        return Result.error(0, "没有查询到相关数据");
    }

    /**
     * 根据负责人名称模糊查找
     * @param name
     * @return
     */
    @GetMapping("/findByName/{name}")
    public Result<?> findByName(@PathVariable String name) {
        LambdaQueryWrapper<CooperativeFarm> wrapper = Wrappers.<CooperativeFarm>lambdaQuery();
        wrapper.like(CooperativeFarm::getName,name);
        List<CooperativeFarm> cooperativeFarmList = cooperativeFarmSurperMapper.selectList(wrapper);
        if(cooperativeFarmList.size() > 0)
            return Result.success(cooperativeFarmList);
        return Result.error(0, "没有查询到相关数据");
    }


    /**
     * 删除
     * @param id
     * @return
     */
    @DeleteMapping("/delById/{id}")
    public Result<?> delete(@PathVariable String id) {
        int res = cooperativeFarmSurperMapper.deleteById(id);
        if(res > 0)
            return Result.success();
        return Result.error(0, "删除失败");
    }

}
